<?php 
/**
* @Enterprise: S&S Media Solutions
* @author: Yannick Spang
* @creation date: April 2009
* @url: http://www.joomla-virtuemart-designs.com
* @copyright: Copyright (C) 2008 - 2009 S&S Media Solutions
* @license: Commercial, see LICENSE.php
* @product: Flypage Admirable - Virtuemart Theme
* @version: 1.0
*/

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
mm_showMyFileName(__FILE__);
global $db;
$image_id = intval(vmGet($_REQUEST, "image_id", "product"));

if( !empty($product_id) ) {

  require_once( CLASSPATH . "ps_product.php" );
  $ps_product =& new ps_product();
  
// Get the default full and thumb image
  $db->query( "SELECT product_name,product_full_image,product_thumb_image FROM #__{vm}_product WHERE product_id='$product_id'");
  $db->next_record();
  $alt = $db->f("product_name");
  $height = PSHOP_IMG_HEIGHT;
  $width = PSHOP_IMG_WIDTH;
  $href = 'index.php?page=shop.product_details&amp;flypage='.$flypage.'&amp;product_id='.$product_id.'&amp;category_id='.$category_id.'&amp;option=com_virtuemart&amp;Itemid='.$Itemid;
  $title = $db->f("product_name");
  echo "<a href=\"$href\" target=\"_self\" title=\"$title\">\n";
  $ps_product->show_image( $db->f("product_thumb_image"), "alt=\"$alt\"");
  echo "</a>\n";
  $dbi = new ps_DB();
// Let's have a look wether the product has more images.
  $dbi->query( "SELECT * FROM #__{vm}_product_files WHERE file_product_id='$product_id' AND file_is_image='1'" );
  $images = $dbi->record;
  $i = 0;
  foreach( $images as $image ) {
    $info = pathinfo( $image->file_name );
    
	$base_filename = basename($image->file_name, ".".$info["extension"])."_".$image->file_image_thumb_height."x".$image->file_image_thumb_width.".".$info["extension"];
	$thumb = $mosConfig_absolute_path.'/'.$info["dirname"] ."/resized/". $base_filename;
	$thumburl = str_replace( $mosConfig_absolute_path, $mosConfig_live_site, $thumb );
	
    $alt = $image->file_title;
    $height = empty($image->file_image_thumb_height) ? PSHOP_IMG_HEIGHT : $image->file_image_thumb_height; 
    $width = empty($image->file_image_thumb_width) ? PSHOP_IMG_WIDTH : $image->file_image_thumb_width; 
    
    $href = 'index.php?page=shop.product_details&amp;flypage='.$flypage.'&amp;product_id='.$product_id.'&amp;category_id='.$category_id.'&amp;option=com_virtuemart&amp;Itemid='.$Itemid.'&amp;image_id='.$image->file_id;
    $title = $image->file_title;
    echo "<a href=\"$href\" target=\"_self\" title=\"$title\"><img src=\"$thumburl\" alt=\"$alt\"  width=\"$width\" /></a>\n";
    // Break Row when needed
    //if( $i++ >= 4 ) { $i=0; echo "<br/><br/>"; }
  }
  echo "<br/>\n";
  
  if( $image_id == "product" ) {
    echo "<div id=\"ssmed_zoom_image\">";
    $ps_product->show_image($db->f("product_full_image"), "alt=\"$alt\" ", 0);
    echo "</div>";
  }
  else {
    if( !empty($image_id) ) {
      // Get that image!
      $dbi->query( "SELECT * FROM #__{vm}_product_files WHERE file_product_id='$product_id' AND file_is_image='1' AND file_id='$image_id'" );
    }
    else {
      // Get the first image
      $dbi->query( "SELECT * FROM #__{vm}_product_files WHERE file_product_id='$product_id' AND file_is_image='1'" );
    }
    $show_img = $dbi->record[0];
    if( $show_img ) {
    	$filename = $mosConfig_absolute_path.str_replace( $mosConfig_absolute_path, '', $show_img->file_name );
      	$src = str_replace( $mosConfig_absolute_path, $mosConfig_live_site, $filename );
      	if( strstr( $src, $mosConfig_live_site.$show_img->file_name)) {
      		$src = str_replace( $mosConfig_live_site.$show_img->file_name, $mosConfig_live_site."/".$show_img->file_name, $src );
      	}
      	$alt = $show_img->file_title;
      	$height = $show_img->file_image_height; 
      	$width = $show_img->file_image_width;
      	echo "<div id=\"ssmed_zoom_image\"><img class=\"zoomed_image\"src=\"$src\" alt=\"$alt\" /></div>";
    }
    else {
      echo $VM_LANG->_('PHPSHOP_IMAGE_NOT_FOUND');
    }
  }
}
?>